﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ClientRegister
{
    public partial class newStudentRegisterClassForm : Form
    {

        SynchronizeData sync = new SynchronizeData();

        GetNextID getId = new GetNextID();

        public newStudentRegisterClassForm()
        {
            InitializeComponent();
        }


        private void newStudentRegisterClass_Load(object sender, EventArgs e)
        {

            ////Nhan du lieu Hoc vien trong cot InsertData tu webservice
            //sync.GetDataForInsertMethodWithID(sync.studentTb, sync.insertcol);

            ////Nhan du lieu hoc vien trong cot UpdateData tu webservice
            //sync.GetDataForUpdateMethodWithID(sync.studentTb, sync.updatecol);

            ////Nhan du lieu thong tin lop hoc cua hoc vien tu server trong cot insert
            //sync.GetDataForInsertMethodWithNoneID(sync.ClassOfStu, sync.insertcol);

            ////Nhan du lieu thon tin hoc vien trong cot update tu server
            //sync.GetDataForUpdateMethodWithNoneID(sync.ClassOfStu, sync.updatecol);

            loadComboGender();
    //Load du lieu danh sach khoa hoc vao Combobox
            CourseDataProvider course = new CourseDataProvider();
            DataSet dscourse = course.loadCourses();
            cbbCourse.DataSource = dscourse.Tables[0];
            cbbCourse.DisplayMember = "NameCourse";
            cbbCourse.ValueMember = "ID";
        }


        private void button1_Click(object sender, EventArgs e)
        {           
            this.Close();
        }
    //Load du lieu gioi tinh vao combobox Gioi tinh
        public void loadComboGender()
        {

            GenderDataProvider gender = new GenderDataProvider();
            DataSet ds = gender.loadDs();
            comboBox_Gender.DataSource = ds.Tables[0];
            comboBox_Gender.DisplayMember = "Gender";
            comboBox_Gender.ValueMember = "IDGender";
        }
        
     
        //Dang ky hoc vien
        private void button2_Click(object sender, EventArgs e)
        {

            ////Truoc khi thuc hien thao tac dang ky thi can thuc hien viec cap nhat du lieu vao database
            ////Nhan du lieu Hoc vien trong cot InsertData tu webservice
            
            //sync.GetDataForInsertMethodWithID(sync.studentTb, sync.insertcol);

            ////Nhan du lieu hoc vien trong cot UpdateData tu webservice
            //sync.GetDataForUpdateMethodWithID(sync.studentTb, sync.updatecol);

            ////Nhan du lieu thong tin lop hoc cua hoc vien tu server trong cot insert
            //sync.GetDataForInsertMethodWithNoneID(sync.ClassOfStu, sync.insertcol);

            ////Nhan du lieu thon tin hoc vien trong cot update tu server
            //sync.GetDataForUpdateMethodWithNoneID(sync.ClassOfStu, sync.updatecol);

            string name = txtFullName.Text.ToString();
            string birthday = txtBirthday.Text.ToString();
            string job = txtJob.Text.ToString();
            string email = txtEmail.Text.ToString();
            int phone = int.Parse(txtPhone.Text.ToString());
            string address = txtAddress.Text.ToString();

            int idgender = int.Parse(comboBox_Gender.SelectedValue.ToString());

            string lasId = getId.getLastID(getId.studentTB, getId.IDCol);
            string nextId = getId.NextID(lasId, getId.prefixID);

            StudentDataProvider std = new StudentDataProvider();
            int result = std.insertStudent(nextId,name, idgender, birthday, job, phone, email, address);

            //string xmlStu = std.SelectStudentsAndConvertToXMLString(id);
            //Lay id cua hoc vien
            string id = ""; 
            int result2 = 0;
            if(result != 0)
            {
                StudentInClassDataProvider stdClass = new StudentInClassDataProvider();
                result2 = stdClass.InsertStudentInClass(nextId, cbbClass.SelectedValue.ToString());
            }        

            //string xmlStuClass = stdClass.selectClassOfStu(id, int.Parse(cbbClass.SelectedValue.ToString()));

            ////=======Chuyen du Hoc vien lieu len webservice================================
            //    //SynchronizeData sync = new SynchronizeData();
            //    //Chuyen thong tin hoc vien
            //sync.transfDataForInsertMethod(xmlStu, sync.studentTb);
            //    //Chuyen thong tin lop hoc cua hoc vien
            //sync.transfDataForInsertMethod(xmlStuClass, sync.ClassOfStu);
            //====================================================================

            if (result2 != 0)
                MessageBox.Show("Đăng ký thành công");
            else MessageBox.Show("Đăng ký không thành công");
        }

        private void button_Clear_Click(object sender, EventArgs e)
        {
            //SynchronizeData sync = new SynchronizeData();
            //sync.transfDataStudent();

            txtAddress.Clear();
            txtBirthday.Clear();
            txtEmail.Clear();
            txtFullName.Clear();
            txtJob.Clear();
            txtPhone.Clear();
        ////Nhan du lieu Hoc vien trong cot InsertData tu webservice
        //    sync.GetDataForInsertMethodWithID(sync.studentTb, sync.insertcol);

        ////Nhan du lieu hoc vien trong cot UpdateData tu webservice
        //    sync.GetDataForUpdateMethodWithID(sync.studentTb, sync.updatecol);

        ////Nhan du lieu thong tin lop hoc cua hoc vien tu server trong cot insert
        //    sync.GetDataForInsertMethodWithNoneID(sync.ClassOfStu, sync.insertcol);

        ////Nhan du lieu thon tin hoc vien trong cot update tu server
        //    sync.GetDataForUpdateMethodWithNoneID(sync.ClassOfStu, sync.updatecol);
        }

        private void button3_Click(object sender, EventArgs e)
        {

        }

    //Hien thi danh sach lop hoc
        private void cbbCourse_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbbCourse.SelectedValue.ToString() == "System.Data.DataRowView") return;
            else
            {
                ClassDataProvider cls = new ClassDataProvider();
                DataSet dsclass = cls.loadClass(cbbCourse.SelectedValue.ToString());
                cbbClass.DataSource = dsclass.Tables[0];
                cbbClass.DisplayMember = "NameClass";
                cbbClass.ValueMember = "ID";
            }
        }

      // Hien thi thong tin cua lop hoc
        private void cbbClass_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbbClass.SelectedValue.ToString() == "System.Data.DataRowView") return;
            else
            {
                ClassDataProvider cls = new ClassDataProvider();
                SqlDataReader sdr = cls.ClassDetail(cbbClass.SelectedValue.ToString());
                if (sdr.Read())
                {
                    txtFee.Text = sdr.GetValue(0).ToString();
                    txtStudyDay.Text = sdr.GetValue(1).ToString();
                    txtTime.Text = sdr.GetValue(2).ToString();
                    txtStartDate.Text = (DateTime.Parse(sdr.GetValue(3).ToString())).ToShortDateString();
                    txtEndDate.Text = (DateTime.Parse(sdr.GetValue(4).ToString())).ToShortDateString();
                }
                sdr.Close();
            }
        }

    }
}
